/**
 * 武汉天瑞兴科技有限公司
 */
package com.adds.lvds.service.impl.organize;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.adds.lvds.dao.impl.organize.OrganizeInfoMapper;
import com.adds.lvds.model.organize.SysOrgInfo;
import com.adds.lvds.model.organize.SysUserPermission;

/**
 * 项目名称 ：lvds-service-impl
 * 
 * @author zhangjian
 * @time 2017-11-23下午3:52:40
 */
@Service("organizeTreeService")
@Transactional(rollbackFor = Exception.class)
public class OrganizeTreeService {
	@Autowired
	private OrganizeInfoMapper organizeInfoMapper;
	@Autowired
	private SysUserPermissionService sysUserPermissionService;
	// 1'-机构
	private static final String type = "1";

	public List<Object> getOrgJson(String user_id,String Org_id ,boolean initFlag) {
		List<Object> treeList = new ArrayList<Object>();
		List<Object> childList = new ArrayList<Object>();
		SysUserPermission sysUserPermission = null;
		if(StringUtils.isNotEmpty(user_id)){
		List<SysUserPermission> list = sysUserPermissionService
				.findSysUserApplicationsByUserId(Integer.parseInt(user_id),
						type);
		if (list.size() > 0 && list != null)
			sysUserPermission = list.get(0);
		}
		try {
			
			// 初始化时 自动添加根节点及下级机构
			if (initFlag) {
				SysOrgInfo sysOrgInfo_parent = organizeInfoMapper
						.findCurrentOrgAndChildOrgs(Org_id);
				if(sysOrgInfo_parent!=null){
				JSONObject orgJsonObject_parent = new JSONObject();
				orgJsonObject_parent.put("id", sysOrgInfo_parent.getOrg_id());
				orgJsonObject_parent.put("name", sysOrgInfo_parent.getOrg_name());
				orgJsonObject_parent.put("pId", sysOrgInfo_parent.getParent_org_id());
				if (sysUserPermission!=null&&sysOrgInfo_parent.getOrg_id()==sysUserPermission.getOrg_id()){
					orgJsonObject_parent.put("checked", true);
				    orgJsonObject_parent.put("open", true);
				    orgJsonObject_parent.put("show_switch", false);
				}else{
					 orgJsonObject_parent.put("show_switch", true);
				}
				
				// 下级机构
				List<SysOrgInfo> listSysOrgTemp = new ArrayList<SysOrgInfo>();
				listSysOrgTemp = sysOrgInfo_parent.getChildOrgs();
				if (listSysOrgTemp.size() > 0) {
					orgJsonObject_parent.put("isParent", true);
					for (SysOrgInfo sysOrgInfoTemp : listSysOrgTemp) {
						JSONObject orgObject = new JSONObject();
						orgObject.put("id", sysOrgInfoTemp.getOrg_id());
						orgObject.put("name", sysOrgInfoTemp.getOrg_name());
						orgObject.put("pId",
								sysOrgInfoTemp.getParent_org_id());
						if (sysUserPermission!=null&&sysOrgInfoTemp.getOrg_id()==sysUserPermission.getOrg_id()) {
							orgObject.put("checked", true);
							orgObject.put("show_switch", false);
						}else{
							orgObject.put("show_switch", true);
						}
						if (organizeInfoMapper.findChildOrgByParentOrgId(
								String.valueOf(sysOrgInfoTemp.getOrg_id()))
								.size() > 0) {
							orgObject.put("isParent", true);
						}
						childList.add(orgObject);
					}
				}
				JSONArray childJson = JSONArray.fromObject(childList);
				orgJsonObject_parent.put("children", childJson);
				treeList.add(orgJsonObject_parent);
				}
			} else {
				// 查询当前单位包含的成员和包含的下级机构
				SysOrgInfo sysOrgInfo_temp = organizeInfoMapper
						.findSysOrgAndUserByOrgId(Org_id);
				// 机构
				if (sysOrgInfo_temp.getChildOrgs().size() > 0) {
					List<SysOrgInfo> orgList = sysOrgInfo_temp.getChildOrgs();
					for (SysOrgInfo sysOrgInfoObject : orgList) {
						JSONObject jsonObject = new JSONObject();
						jsonObject.put("id", sysOrgInfoObject.getOrg_id());
						jsonObject.put("name", sysOrgInfoObject.getOrg_name());
						jsonObject.put("pId", sysOrgInfoObject.getParent_org_id());
						if (sysUserPermission!=null&&sysOrgInfoObject.getOrg_id()==sysUserPermission.getOrg_id()){
							jsonObject.put("checked", true);
							jsonObject.put("show_switch", false);
						}else{
							jsonObject.put("show_switch", true);
						}
						if (organizeInfoMapper.findChildOrgByParentOrgId(
								String.valueOf(sysOrgInfoObject.getOrg_id()))
								.size() > 0) {
							jsonObject.put("isParent", true);
						}
						treeList.add(jsonObject);
					}

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return treeList;
	}
	
}
